package com.aaa.whj.service.impl;

import com.aaa.whj.dao.IIndexDao;

import com.aaa.whj.entity.TabRecord;
import com.aaa.whj.entity.Tab_scl_record;
import com.aaa.whj.service.IIndexService;
import com.alibaba.fastjson.JSONArray;
import com.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class IndexServiceImpl implements IIndexService {
    SqlSession session= SqlSessionFactoryUtil.getSession();
    IIndexDao indexDao=session.getMapper(IIndexDao.class);

    @Override
    public int addScl_topic(String data) {
        List<TabRecord> tabRecordList= JSONArray.parseArray(data, TabRecord.class);
        for (int i=0;i<tabRecordList.size();i++){
            if(tabRecordList.get(i).getResult().equals("1")){
                tabRecordList.get(i).setResult("无");
            }
            if(tabRecordList.get(i).getResult().equals("2")){
                tabRecordList.get(i).setResult("轻度");
            }
            if(tabRecordList.get(i).getResult().equals("3")){
                tabRecordList.get(i).setResult("中度");
            }
            if(tabRecordList.get(i).getResult().equals("4")){
                tabRecordList.get(i).setResult("偏重");
            }
            if(tabRecordList.get(i).getResult().equals("5")){
                tabRecordList.get(i).setResult("严重");
            }
        }
        return this.indexDao.addScl_topic(tabRecordList);
    }

    @Override
    public int updateRecord(String data) {
        Tab_scl_record record = record(data);
        return this.indexDao.updataRecord(record);
    }

    public Tab_scl_record record(String data){
        int[] F1={1,4,12,27,40,42,48,49,52,53,56,58};
        int[] F2={3,9,10,28,38,45,46,51,55,65};
        int[] F3={6,21,34,36,37,41,61,69,73};
        int[] F4={5,14,15,20,22,26,29,30,31,32,54,71,79};
        int[] F5={2,17,23,33,39,57,72,78,80,86};
        int[] F6={11,24,63,67,74,81};
        int[] F7={13,25,47,50,70,75,82};
        int[] F8={8,18,43,68,76,83};
        int[] F9={7,16,35,62,77,84,85,87,88,90};
        int[] F10={13,25,47,50,70,75,82};
        float F11=0;
        float F22=0;
        float F33=0;
        float F44=0;
        float F55=0;
        float F66=0;
        float F77=0;
        float F88=0;
        float F99=0;
        float F100=0;

        List<TabRecord> tabRecordList= JSONArray.parseArray(data, TabRecord.class);

        for (TabRecord tabRecord:tabRecordList){
            Integer sclId = tabRecord.getSclId();
            String result = tabRecord.getResult();
            int score=0;
            if(result.equals("1")){
                score=1;
            }
            if(result.equals("2")){
                score=2;
            }
            if(result.equals("3")){
                score=3;
            }
            if(result.equals("4")){
                score=4;
            }
            if(result.equals("5")){
                score=5;
            }
            if(su(F1,sclId)){
                F11=F11+score;
            }
            if(su(F2,sclId)){
                F22=F22+score;
            }
            if(su(F3,sclId)){
                F33=F33+score;
            }
            if(su(F4,sclId)){
                F44=F44+score;
            }
            if(su(F5,sclId)){
                F55=F55+score;
            }
            if(su(F6,sclId)){
                F66=F66+score;
            }
            if(su(F7,sclId)){
                F77=F77+score;
            }
            if(su(F8,sclId)){
                F88=F88+score;
            }
            if(su(F9,sclId)){
                F99=F99+score;
            }
            if(su(F10,sclId)){
                F100=F100+score;
            }
        }
        return new Tab_scl_record(tabRecordList.get(1).getId(),null,null,null,null,null,
                F11/12f,F22/10f,F33/9f,F44/13f,F55/10f,F66/6f,F77/7f,F88/6f,F99/10f,F100/7f);
    }
    private boolean su(int[] ints,int id){
        boolean b=false;
        for (int anInt : ints) {
            if (anInt == id) {
                b = true;
                break;
            }
        }
        return b;
    }
}
